<template>
  <view class="content">
    <u-sticky h5-nav-height="0">
      <pageHeader
        :items="tab"
        :index="status"
        @change="(e) => handle('tab', e)"
      >
      </pageHeader>
    </u-sticky>

    <pageList :items="items" :tab="tab" @handle="handle"></pageList>
    <toolsMore :status="moreStatus"></toolsMore>
  </view>
</template>

<script>
import toolsMore from "@/components/tools/tools-more.vue";
import { cutPriceOrderList } from "@/api/marketing/curPrice";
import pageHeader from "./components/page-header.vue";
import pageList from "./components/page-list.vue";

export default {
  components: {
    toolsMore,
    pageHeader,
    pageList,
  },

  data() {
    return {
      status: -1,
      tab: [
        { name: "全部", id: -1 },
        { name: "进行中", id: 0 },
        { name: "已结束", id: 1 },
        { name: "已失败", id: 2 },
        { name: "待付款", id: 10 },
        { name: "已付款", id: 11 },
      ],

      items: [],
      isEnd: false,
      limit: 10,
      page: 1,
      moreStatus: 1,
    };
  },

  onLoad() {
    this.getList();
  },

  methods: {
    async clear() {
      this.items = [];
      this.isEnd = false;
      this.page = 1;

      this.getList();
    },

    handle(type, item) {
      switch (type) {
        case "tab":
          this.status = item;
          this.clear();
          break;

        case "detail":
          this.$utils.toUrl(`/marketing/curPrice/active/index?id=${item.id}`);
          break;

        case "buyNow":
          this.$utils.toUrl(
            `/order/order-detail/order-detail?id=${item.used_order_id}`
          );
          break;
      }
    },

    async getList() {
      let { limit, page, status } = this;
      let res = await cutPriceOrderList(
        {
          limit,
          page,
          status,
        },
        true
      );
      this.moreStatus = 1;
      uni.stopPullDownRefresh();
      if (!res.code) {
        let { items: oldList } = this,
          { totalCount, list } = res.data;
        if (list.length) {
          oldList.push(...list);
          this.page = this.page + 1;
        }
        if (oldList.length >= parseInt(totalCount)) {
          this.isEnd = true;
          this.moreStatus = 3;
        }
      }
    },
  },

  onReachBottom() {
    if (this.isEnd) {
      return;
    }

    this.getList();
  },

  onPullDownRefresh() {
    this.clear();
  },
};
</script>

<style lang="less" scoped>
.content {
  min-height: 100vh;
  background: #f2f2f2;
}
</style>
